#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

 struct ListNode {
      int val;
      struct ListNode *next;
  };
  struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{

    struct ListNode *curA=headA;
    int Ai=0;
    while(curA)
    {
        Ai++;
        curA=curA->next;
    }
    struct ListNode *curB=headB;
    int Bi=0;
    while(curB)
    {
        Bi++;
        curB=curB->next;
    }
    struct ListNode * longLN=headA;
    struct ListNode * shortLN=headB;
    if(Ai<Bi)
    {
    longLN=headB;
    shortLN=headA;
    }

    int gap=abs(Ai-Bi);
    while(gap--)
    {
        longLN=longLN->next;
    }
    while(longLN)
    {
        if(longLN==shortLN)
        {
            return longLN;
        }
        else
        {
            shortLN=shortLN->next;
            longLN=longLN->next;
        }
    }
    return NULL;

}